package com.yinhe.music.yhmusic.personal;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hwangjr.rxbus.RxBus;
import com.yinhe.music.common.GlideHelper.GlideHelper;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.base.BaseFragment;
import com.yinhe.music.yhmusic.log.BlankUtil;
import com.yinhe.music.yhmusic.model.Music;
import com.yinhe.music.yhmusic.personal.vip.MyVipActivity;
import com.yinhe.music.yhmusic.utils.ViewUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by 何晓华 on 2017/9/20.
 */

public class BuyMusicFragment extends BaseFragment {
    @BindView(R.id.song_img)
    ImageView songImg;
    @BindView(R.id.song_name)
    TextView songName;
    @BindView(R.id.song_price)
    TextView songPrice;
    @BindView(R.id.singer_name)
    TextView singerName;
    @BindView(R.id.album_name)
    TextView albumName;
    Unbinder unbinder;
    @BindView(R.id.open_vip)
    TextView openVip;
    private Music music;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_buy_music, container, false);
        unbinder = ButterKnife.bind(this, view);
        RxBus.get().register(this);
        if (getArguments() != null) {
            music = (Music) getArguments().getSerializable("music");
        }
        initView();
        return view;
    }

    @Override
    public void onDestroyView() {
        RxBus.get().unregister(this);
        super.onDestroyView();
        unbinder.unbind();
    }

    private void initView() {
        if (music == null){
            return;
        }

        if (!BlankUtil.isBlank(music.getSongName())){
            songName.setText(String.format("单曲《%s》", music.getSongName()));
        }

        String price = String.valueOf(music.getPrice());
        songPrice.setText(ViewUtils.setStyleText(price,"", getActivity(), null, null, 20, 12,"雪币" ));
        singerName.setText(String.format("歌手：%s", music.getSingerName()));
        if (music.getAlbumName()!=null&&music.getAlbumName().length() > 0) {
                albumName.setText(String.format("专辑：%s", music.getAlbumName()));
            } else {
                albumName.setVisibility(View.GONE);
            }

        GlideHelper.setImageResource(songImg,music.getImage(),R.drawable.default_cover);
        openVip.setOnClickListener(view -> {
            Intent intent = new Intent(getActivity(), MyVipActivity.class);
            startActivity(intent);
        });
    }
}
